package com.jojo.jojomall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jojo.common.utils.BeanCopyUtils;
import com.jojo.common.utils.PageUtils;
import com.jojo.common.utils.Query;
import com.jojo.jojomall.product.dao.AttrAttrgroupRelationDao;
import com.jojo.jojomall.product.entity.AttrAttrgroupRelationEntity;
import com.jojo.jojomall.product.entity.AttrEntity;
import com.jojo.jojomall.product.service.AttrAttrgroupRelationService;
import com.jojo.jojomall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;



import com.jojo.jojomall.product.dao.AttrGroupDao;
import com.jojo.jojomall.product.entity.AttrGroupEntity;
import com.jojo.jojomall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
@Autowired
private AttrAttrgroupRelationDao relationDao;
@Autowired
private AttrGroupDao attrGroupDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }
@Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
    String key = (String) params.get("key");
    QueryWrapper<AttrGroupEntity> ageqw = new QueryWrapper<AttrGroupEntity>().eq("attr_group_id",catelogId);
        if (catelogId==0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), ageqw);
            return new PageUtils(page);
        }else{
            if (!StringUtils.isEmpty(key)){
                ageqw.and(obj->{obj.eq("attr_group_id",key).or().like("attr_group_name",key);});
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),ageqw);
            return new PageUtils(page);

        }
    }
//删除属性与分组的关联关系
    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
        //把vos转为list集合方便stream
        List<AttrGroupRelationVo> attrGroupRelationVos = Arrays.asList(vos);
        List<AttrAttrgroupRelationEntity> agrel = BeanCopyUtils.copyBeanList(attrGroupRelationVos, AttrAttrgroupRelationEntity.class);

        relationDao.deleteBatchReleations(agrel);
    }
//    获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
    @Override
    public PageUtils noattrRelation(Map<String, Object> params, Long attrgroupId) {
//        当前分组只能关联自己所属的分类里面的所有属性。
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        //拿到这组的分类id
        Long catelogId = attrGroupEntity.getCatelogId();
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));

        List<Long> groupidList = attrGroupEntities.stream().map(item ->
                item.getAttrGroupId()
        ).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = relationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrGroupId, groupidList));
        List<Long> collect = attrAttrgroupRelationEntities.stream().map(item -> item.getAttrId()).collect(Collectors.toList());


        List<AttrGroupEntity> attrGroupEntities1 = baseMapper.selectList(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId).notIn(AttrGroupEntity::getAttrGroupId, collect));
      QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id",catelogId);
        String key = (String) params.get("key");
        if (!StringUtils.hasText(key)){
            wrapper.and(w->w.eq("attr_id",key).or().like("attr_name",key));
        }
//        IPage page = this.page(new Query<AttrEntity>().getPage(params),wrapper);

        return  null;
    }

}
